package com.eco.config;

import com.eco.constants.Constants;
import com.eco.dao.ds3.FishTypeMapper;
import com.eco.entity.FishType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

/**
 * @Author : xulinyu
 * @Date: 2024/6/26 14:32
 * @Description: 初始化鱼种信息
 */
@Configuration
@Slf4j
public class FishTypeInitConfig implements CommandLineRunner {

    private final FishTypeMapper fishTypeMapper;

    private final StringRedisTemplate stringRedisTemplate;

    public FishTypeInitConfig(FishTypeMapper fishTypeMapper, StringRedisTemplate stringRedisTemplate) {
        this.fishTypeMapper = fishTypeMapper;
        this.stringRedisTemplate = stringRedisTemplate;
    }

    private void initFishType() {
        List<FishType> fishTypeList = fishTypeMapper.findAllTypeByStationName();
        fishTypeList.forEach(x->{
            stringRedisTemplate.opsForValue().set(Constants.FISH_TYPE_REDIS_KEY+x.getFishType(),x.getFishName());
        });
        log.info("鱼种信息初始化成功!");
    }

    @Override
    public void run(String... args) throws Exception {
        initFishType();
    }
}
